window.onload = function ()
{
	let submit_center = document.getElementById('submit_center'); //元素定位到提交按钮
	let c_p = document.getElementById('c_p'); //元素定位到span上的
	let form_message = document.getElementById('form_message');//
	let textareaWaH = document.getElementById('textareaWaH');//
	let textarea = document.getElementById('textareaWaH');//
	let submit_1 = document.getElementById('submit_1');
	let winW_H = setInterval(()=>{  //设置定时器每隔1秒钟运行一次
	let windowWidth = window.innerWidth,
	    windowHeight = window.innerHeight;
	if(typeof(windowWidth) == "number" && typeof(windowHeight) == "number")
	{
		
		if(windowWidth < 800 || windowHeight < 400) //判断浏览器的宽高
		{
			
			c_p.innerHTML = "<br />";
		}
		else
		{
			
			c_p.innerHTML = "";
		}
	}
	else //如果获取不到浏览器的大小
	{
		console.log(`获取不到浏览器窗口大小`);
		clearInterval(winW_H);
	}
	
	},1000);
	
	
	
	submit_1.addEventListener('click',submit_up);
	
	//提交判断函数
	function submit_up()
	{
		let value1 = textarea.value;
		  console.log("值为:"+value1);
		  console.log("长度为:"+value1.length);
		  if(value1 == "")
		  {
		  	console.log("内容为:"+textarea.value)
		  	alert("当前内容为空，不允许提交");
		  }
		  else
		 {
			if(confirm(`确定提交吗?`))
			{
				let xhr = new XMLHttpRequest();  //建立一个异步对象
				xhr.open("POST","./PHP/insmessage.php",true); // 设置传输方式 post 或 get 把第三个参数设置为true或false true为异步 false为同步 不过我们使用ajax就是为了异步而来的 所以要设为true
				xhr.setRequestHeader("Content-type","application/x-www-form-urlencoded"); // post要设置请求头
				xhr.send("newMessage="+value1); // post在这传参
				xhr.onreadystatechange = function ()  //当发生改变时 执行这个
				{
							 if(xhr.readyState == 4)  //是否已经完成了请求
							 {
								 if(xhr.status >= 200 && xhr.status < 300 || xhr.status == 304) //判断状态码
								 {
									 alert(xhr.responseText);
								 }
								 else
								 {
									 alert("提交失败");
								 }
							 }
				}
				
			}
			else
			{
				alert('你已取消提交');
			}
		
		}
	}
	
	
	textareaWaH.addEventListener("textInput",(event)=>{
		// alert(`你输入了文本：${event.data}`);
		let random_1 = Math.floor( Math.random()*10 );
		console.log(`当前的随机数为${random_1}`);
		switch(random_1)
		{
			case 1:
			submit_1.style.backgroundColor = "#F78888";
			break;
			case 2:
			submit_1.style.backgroundColor = "#F3D250";
			break;
			case 3:
			submit_1.style.backgroundColor = "#FFCCCC";
			break;
			case 4:
			submit_1.style.backgroundColor = "#CCFF99";
			break;
			case 5:
			submit_1.style.backgroundColor = "#CCCCFF";
			break;
			case 6:
			submit_1.style.backgroundColor = "#77A6F7";
			break;
			case 7:
			submit_1.style.backgroundColor = "#FFCCBC";
			break;
			case 8:
			submit_1.style.backgroundColor = "#FFCB9A";
			break;
			case 9:
			submit_1.style.backgroundColor = "#D1E8E2";
			break;
			default:
			submit_1.style.backgroundColor = "#bb6e78"
		}
		
	})
	
	
	
}